<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>个人信息 - 医院陪护系统</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
        .avatar-img {
            width: 150px;
            height: 150px;
            object-fit: cover;
            border-radius: 50%;
        }
        .profile-card {
            border-radius: 15px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
        }
    </style>
</head>
<body>
<div class="container py-5">
    <div class="row">
        <div class="col-md-4">
            <div class="card profile-card mb-4">
                <div class="card-body text-center">
                    <img src="upload/avatar.jpg" alt="头像" class="avatar-img mb-3" id="userAvatar">
                    <h4 id="1">张三</h4>
                    <p class="text-muted" id="userRole">普通用户</p>
                    <input type="file" id="avatarUpload" class="d-none" accept="image/*">
                    <button class="btn btn-outline-primary btn-sm" onclick="document.getElementById('avatarUpload').click()">
                        更换头像
                    </button>
                </div>
            </div>
        </div>
        <div class="col-md-8">
            <div class="card profile-card">
                <div class="card-header">
                    <h5>个人信息</h5>
                </div>
                <div class="card-body">
                    <form id="profileForm">
                        <div class="row mb-3">
                            <div class="col-md-6">
                                <label class="form-label">用户名</label>
                                <input type="text" class="form-control" id="username" readonly>
                            </div>
                            <div class="col-md-6">
                                <label class="form-label">真实姓名</label>
                                <input type="text" class="form-control" id="realName" name="realName">
                            </div>
                        </div>
                        <div class="row mb-3">
                            <div class="col-md-6">
                                <label class="form-label">性别</label>
                                <select class="form-select" id="gender" name="gender">
                                    <option value="male">男</option>
                                    <option value="female">女</option>
                                </select>
                            </div>
                            <div class="col-md-6">
                                <label class="form-label">出生日期</label>
                                <input type="date" class="form-control" id="birthday" name="birthday">
                            </div>
                        </div>
                        <div class="row mb-3">
                            <div class="col-md-6">
                                <label class="form-label">手机号</label>
                                <input type="tel" class="form-control" id="phone" readonly>
                            </div>
                            <div class="col-md-6">
                                <label class="form-label">邮箱</label>
                                <input type="email" class="form-control" id="email" name="email">
                            </div>
                        </div>
                        <div class="mb-3">
                            <label class="form-label">身份证号</label>
                            <input type="text" class="form-control" id="idCard" readonly>
                        </div>
                        <div class="d-grid gap-2 d-md-flex justify-content-md-end">
                            <button type="button" class="btn btn-primary" onclick="updateProfile()">保存修改</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
<script>
    // 获取用户ID - 实际项目中应从登录状态获取
    const userId = 1;

    // 页面加载时获取用户信息
    document.addEventListener('DOMContentLoaded', function() {
        fetchUserInfo();
    });

    // 获取用户信息
    function fetchUserInfo() {
        axios.get(`/api/user/info?userId=${userId}`)
            .then(response => {
                const user = response.data;
                document.getElementById('userName').textContent = user.realName;
                document.getElementById('username').value = user.username;
                document.getElementById('realName').value = user.realName;
                document.getElementById('gender').value = user.gender;
                document.getElementById('phone').value = user.phone;
                document.getElementById('email').value = user.email || '';
                document.getElementById('idCard').value = user.idCard || '';

                if (user.birthday) {
                    const birthday = new Date(user.birthday);
                    const formattedDate = birthday.toISOString().split('T')[0];
                    document.getElementById('birthday').value = formattedDate;
                }

                if (user.avatar) {
                    document.getElementById('userAvatar').src = user.avatar;
                }
            })
            .catch(error => {
                console.error('获取用户信息失败:', error);
                alert('获取用户信息失败，请刷新重试');
            });
    }

    // 更新用户信息
    function updateProfile() {
        const formData = new FormData(document.getElementById('profileForm'));
        const userData = {
            id: userId,
            realName: formData.get('realName'),
            gender: formData.get('gender'),
            email: formData.get('email'),
            birthday: formData.get('birthday')
        };

        axios.post('/api/user/update', userData)
            .then(response => {
                alert('个人信息更新成功');
                fetchUserInfo();
            })
            .catch(error => {
                console.error('更新失败:', error);
                alert('更新失败，请检查输入');
            });
    }

    // 头像上传处理
    document.getElementById('avatarUpload').addEventListener('change', function(e) {
        const file = e.target.files[0];
        if (!file) return;

        const formData = new FormData();
        formData.append('file', file);

        axios.post('/api/upload/avatar', formData, {
            headers: {
                'Content-Type': 'multipart/form-data'
            }
        })
            .then(response => {
                const avatarUrl = response.data.url;
                document.getElementById('userAvatar').src = avatarUrl;
                alert('头像上传成功');
            })
            .catch(error => {
                console.error('头像上传失败:', error);
                alert('头像上传失败');
            });
    });
</script>
</body>
</html>